package com.aijia.custom.product.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aijia.custom.product.R;
import com.aijia.custom.product.data.WSheetRecListData;
import com.aijia.custom.product.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 各到工序
 * <p>
 * Created by yejianjun on 2019/1/21.
 */

public class RecordAdapter extends BaseAdapter implements View.OnClickListener {
    List<WSheetRecListData> mList = new ArrayList<>();
    private Context mContext;
    private LayoutInflater mInflater;
    private int type;
    private OnItemClickListener onItemClickListener;

    public RecordAdapter(Context ctx, int type) {
        mContext = ctx;
        this.type = type;
        mInflater = LayoutInflater.from(ctx);
    }

    public void setData(List<WSheetRecListData> list) {
        mList = list;
    }

    public List<WSheetRecListData> getList() {
        return mList;
    }

    public void removeOneData(int position) {
        mList.remove(position);
    }

    public void addOneData(WSheetRecListData data) {
        mList.add(data);
    }

    public void clear() {
        mList.clear();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (null == convertView) {
            holder = new Holder();
            convertView = mInflater.inflate(R.layout.contact_record_item, null);
            holder.amountTV = convertView.findViewById(R.id.wsheet_art_item_amount);
            holder.dateTV = convertView.findViewById(R.id.wsheet_art_item_time);
            holder.contentTV = convertView.findViewById(R.id.wsheet_art_item_content);
            holder.deleteTV = convertView.findViewById(R.id.wsheet_art_item_delete);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        WSheetRecListData data = mList.get(position);
        holder.dateTV.setText("时间：" + DateUtils.getFormatDate(data.getCreateDate() == null ? "" : data.getCreateDate()));
        holder.contentTV.setText("备注：" + data.getContent());
        holder.amountTV.setText("发起人：" + (data.getStaffName() == null ? "" : data.getStaffName()));
        holder.amountTV.setVisibility(View.VISIBLE);
        holder.deleteTV.setVisibility(View.GONE);

        holder.deleteTV.setTag(R.id.wsheet_art_item_delete, position);

        return convertView;
    }

    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void removeOnItemClickListener() {
        this.onItemClickListener = null;
    }

    @Override
    public void onClick(View v) {
        if (null != onItemClickListener) {
            onItemClickListener.itemClick(v);
        }
    }

    public interface OnItemClickListener {
        void itemClick(View view);
    }


    class Holder {
        public TextView amountTV;
        public TextView dateTV;
        public TextView contentTV;
        public TextView deleteTV;
    }

}
